package com.sky.item.config;


import com.sky.entity.DishFlavor;

import com.sky.item.mapper.DishFlavorMapper;
import com.sky.item.mapper.DishMapper;
import com.sky.item.service.es.DishServiceForES;
import com.sky.item.service.es.DishVOForES;
import com.sky.properties.EsProperties;

import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

import java.io.IOException;
import java.util.List;

@Configuration
//@EnableElasticsearchRepositories(basePackages = {"com.sky.repository"})
@ComponentScan(basePackages = {"com.sky"})
@Slf4j
public class ElasticConfig extends AbstractElasticsearchConfiguration {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private DishServiceForES dishServiceForES;

    @Autowired
    private DishVOForES dishVOForES;

//    @Value("${elasticsearch.host}")
//    public String host;
//    @Value("${elasticsearch.port}")
//    public Integer port;

    /**
     * 重写逻辑为空，因为注入的properties bean会在此bean加载之后注入，因为此bean是父类bean，先被加载
     * @return
     */
    @Override
    public RestHighLevelClient elasticsearchClient() {
//        log.info(host+" "+port);
//        log.info("elasticsearchClient...");
//        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port,"http")));
//        return client;
        return null;
    }

    /**
     * 自定义client bean，因为需要先将esProperties bean注入或者穿参
     * 才能正确获得esProperties的内容
     * @param esProperties
     * @return
     */
    @Bean
    public RestHighLevelClient elasticsearchClient(EsProperties esProperties){
        log.info("elasticsearchClient...");
        log.info(esProperties.getHost()+" "+esProperties.getPort());
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(esProperties.getHost(), esProperties.getPort(),"http")));
        return client;
    }


    @Bean
    public ApplicationRunner dishInitForES(){
        return new ApplicationRunner() {
            @Override
            public void run(ApplicationArguments args) throws Exception {
//                initDishES();
                initDishVoES();
            }
        };
    }

    /**
     * 初始化dishEs
     * @throws IOException
     */
//    public void initDishES() throws IOException {
//        log.info("dishInitForES.....");
//        List<Dish> dishes = dishMapper.selectAll();
//        //1.删除索引中的数据
//        Boolean status = dishServiceForES.deleteAll("dish");
//        //2.初始化索引
//        BulkRequest bulkRequest = new BulkRequest("dish");
//        for (Dish dish : dishes) {
//            bulkRequest.add(
//                    new IndexRequest()
//                            .id(""+dish.getId())
//                            .source(JSON.toJSONString(new ESDish(dish.getId(),dish.getName(),dish.getStatus().longValue())), XContentType.JSON)
//            );
////                    log.info(JSON.toJSONString(dish));
//        }
//        BulkResponse response = elasticsearchClient().bulk(bulkRequest, RequestOptions.DEFAULT);
//        log.info(response.status().name());
//    }

    public void initDishVoES(){
        List<DishVO> dishVOList = dishMapper.selectAllVO();
        dishVOForES.deleteAll();
        for (int i = 0;i<dishVOList.size();i++) {
            //根据菜品id查询口味数据
            List<DishFlavor> dishFlavorList = dishFlavorMapper.getByDishId(dishVOList.get(i).getId());
            dishVOList.get(i).setFlavors(dishFlavorList);
        }
        dishVOForES.saveAll(dishVOList);
        log.info("dishVO init ok ....");
    }

}
